Building Games with Flutter by Paul Teale

Building Games with Flutter by Paul Teale

Author:Paul Teale
Language: eng
Format: epub
Publisher: Packt
Published: 2022-11-15T00:00:00+00:00


Figure 7.3 – Using the terrain images to make a larger map

Notice in Figure 7.3 that a much larger water area was created by reusing the smaller tile images to build something much bigger.

The Tiled tool makes creating tile maps very easy, including tools such as image editing, which allow you to draw with tiles onto the map however you like. You can export maps from Tiled in a variety of formats, but XML and JSON are the most common.

Tiled is a tool that is easy to get started with but also has advanced features beyond the scope of this book, so we won't go into any more detail about it here. Instead, we will provide the premade tile maps for use in our game, so that you can use them directly, but I do recommend spending some time with Tiled once you want to create tile maps for your own games.

When you export a map from Tiled, it has a lot of information about the map and tile size, but the map data section is represented as a 2D array, which could look something like this:

[

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 2, 2, 2, 2, 2, 2, 1],

[1, 2, 3, 3, 3, 3, 2, 1],

[1, 2, 2, 2, 2, 2, 2, 1],

[1, 1, 1, 1, 1, 1, 1, 1]

]

The 2D array in the preceding code block is eight tiles wide and five tiles high. 1 in the array could represent the tile ID for some mountains, 2 might represent a path, and 3 might be water. So, in this example, the map has a path around a lake in the center of the map, with mountains around the outer edges.

Inside a game, we would load this data and iterate over the array and draw each visible tile based on its tile ID. We will draw the tiles starting at the top left, 0,0, and then go through the tile data line by line, drawing each tile in turn.

Now that we have had an introduction to tile maps, we will add our own to the game in the next section.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.